package com.netty.source_code.transport.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

/**
 * @author xl-9527
 * @since 2025/7/15
 **/
public class OutMsgHandler extends SimpleChannelInboundHandler<Object> {

    private static final Logger log = LoggerFactory.getLogger(OutMsgHandler.class);

    @Override
    protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception {
        this.printMsg(msg);
    }

//    @Override
//    public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
//        this.printMsg(msg);
//    }

    private void printMsg(final Object msg) {
        if (msg instanceof ByteBuf) {
            ByteBuf newByteBuf = ((ByteBuf) msg).retain();
            log.info("写的消息：{}", newByteBuf.toString(StandardCharsets.UTF_8));
        }
        log.info("写的消息：{}", msg);
    }
}
